package com.jiai.cloud.sys.controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.entity.GoodSearch;
import com.jiai.cloud.entity.TGoods;
import com.jiai.cloud.entity.TGoodsSpac;
import com.jiai.cloud.sys.service.ITGoodsService;
import com.jiai.cloud.sys.service.ITGoodspacService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商品表 前端
 * 控制器
 * </p>
 *
 * @佳爱科技
 * @since 2017-05-24
 */
@RestController
@RequestMapping("/api/v1/goods")
public class TGoodsController {
    @Autowired
    private ITGoodsService itGoodsService;
    @Autowired
    private ITGoodspacService itGoodspacService;

    //增
    @RequestMapping(value = "/addGoods",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object addGoods(@RequestBody TGoods tGoods)throws ServiceException {
        try{
            itGoodsService.insert(tGoods);
            return RestResultGenerator.genSuccessResult("插入成功");
        }catch (Exception e){
            throw new ServiceException("插入失敗", e.getMessage());
        }
    }

    //通过ID删除
    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object deleteId(@PathVariable("id") Integer id) throws ServiceException{
        try{
            itGoodsService.deleteById(id);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
        public Object update(TGoods goods) throws ServiceException{
        try{

            itGoodsService.updateById(goods);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("编辑失敗", e.getMessage());
        }
    }

    //查所有
    @RequestMapping(value = "/findByAll",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object findByAll(GoodSearch search) throws ServiceException{
        try {
            EntityWrapper wrapper=new EntityWrapper();
            if(!StringUtils.isEmpty(search.getGoodsName())){
                wrapper.like("go.goodsName",search.getGoodsName());
            }
            if(search.getTypeId()!=null){
                wrapper.eq("typeId",search.getTypeId());
            }
            Page page=search.getPagePlus();

            itGoodsService.findGoodsView(page,wrapper);
          return RestResultGenerator.genSuccessResult(new CustomPage(page)) ;
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }





    //根据ID查找
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findById(@PathVariable("id") Integer id) throws ServiceException {
        try {

            return RestResultGenerator.genSuccessResult(itGoodsService.findGoodsFullyById(id));
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }




    //改(有就改，没有就插入)
    @RequestMapping(value="/edit",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object edit(@RequestBody TGoods goods)throws ServiceException{
        try {
            itGoodsService.insertOrUpdate(goods);
            for (TGoodsSpac spac:goods.getSpacs()) {
                spac.setGoodsId(goods.getGoodsId());
            }
            itGoodspacService.insertOrUpdateBatch(goods.getSpacs());
            return null;
        } catch (Exception e) {
            throw new  ServiceException("编辑失敗",e.getMessage());
        }
    }




}
